//
//  CDVAlipay.m
//  X5
//
//  Created by 007slm on 12/8/14.
//
//

#import "CDVAMapLocationPlugin.h"



@implementation CDVAMapLocationPlugin

@synthesize locationManager = _locationManager;

-(void)pluginInitialize{
    CDVViewController *viewController = (CDVViewController *)self.viewController;
    self.userAMapKey = [viewController.settings objectForKey:@"useramapkey"];
    [AMapLocationServices sharedServices].apiKey = self.userAMapKey;
    
    self.locationManager = [[AMapLocationManager alloc] init];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    self.locationManager.delegate = self;
    [self initCompleteBlock];
}

- (void)initCompleteBlock
{
    __weak CDVAMapLocationPlugin *wSelf = self;
    
    self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
    {
        NSDictionary* message ;
        
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            message = [NSDictionary dictionaryWithObjectsAndKeys:
                       [NSString stringWithFormat:@"%ld", (long)error.code],@"errCode",
                       error.localizedDescription,@"errInfo",
                       nil];
            if(wSelf.currentCallbackId) {
                CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:message];
                [wSelf.commandDelegate sendPluginResult:result callbackId:wSelf.currentCallbackId];
            }
            
            return;
        }
        
        if (location)
        {
            
            if(regeocode) {
                message = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSString stringWithFormat:@"%f",location.coordinate.longitude],@"lng",
                           [NSString stringWithFormat:@"%f",location.coordinate.latitude],@"lat",
                           regeocode.adcode, @"adcode",
                           regeocode.formattedAddress, @"address",
                           [NSString stringWithFormat:@"%@%@",regeocode.street,regeocode.number],@"address2",
                           regeocode.formattedAddress, @"name",
                           nil];
            }else {
                message = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSString stringWithFormat:@"%f",location.coordinate.longitude],@"lng",
                           [NSString stringWithFormat:@"%f",location.coordinate.latitude],@"lat",
                           @"0", @"adcode",
                           @"", @"address",
                           @"", @"address2",
                           @"", @"name",
                           nil];
            }
            //NSLog(@"AMap,initCompleteBlock :%@", message);
            if(wSelf.currentCallbackId) {
                CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
                [wSelf.commandDelegate sendPluginResult:result callbackId:wSelf.currentCallbackId];
            }
        }
    };
}


- (void)getAmapLocation:(CDVInvokedUrlCommand*)command {
    // 带逆地理（返回坐标和地址信息）
    self.currentCallbackId = command.callbackId;
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
}

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    //NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
    //NSLog(@"%s, didUpdateLocation = {lat:%f; lon:%f;}", __func__, location.coordinate.latitude, location.coordinate.longitude);
}



@end
